const Controller = require("egg").Controller;
const { success, fail, handle_status } = require("../utils/code");

class MenuController extends Controller {
  async add() {
    const { ctx } = this;
    const body = ctx.request.body;
    const {type, message } = await ctx.service.note.add(body);
    ctx.body = handle_status[type]({}, message)
  }

  async edit() {
    const { ctx } = this;
    const body = ctx.request.body;
    const result = await ctx.service.note.edit(body);
    if (result) {
      ctx.body = success();
    } else {
      ctx.body = fail();
    }
  }

  async list() {
    const { ctx } = this;
    const { keyword, page, page_size } = ctx.request.query;
    const result = await ctx.service.note.list( keyword, page, page_size);
    if (result) {
      ctx.body = success(result);
    } else {
      ctx.body = fail();
    }
  }
  async detail() {
    const { ctx } = this;
    const { id } = ctx.request.query;
    const result = await ctx.service.note.detail_by_id(id);
    if (result) {
      ctx.body = success(result);
    } else {
      ctx.body = fail();
    }
  }

  async delete() {
    const { ctx } = this;
    const body = ctx.request.body;
    const result = await ctx.service.note.delete(body.id);
    if (result) {
      ctx.body = success();
    } else {
      ctx.body = fail();
    }
  }
}
module.exports = MenuController;
